<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 李邦琦 <1536549251@qq.com>
// +----------------------------------------------------------------------

namespace app\crm\model;
use think\Db;
use think\Model;
use think\Cache;
class Announcement extends Model
{
    /**
     * 获取公告列表
     * @param str $where <查询条件>
     * @param int $page <页码>
     * @param int $limit <每页显示条数>
     * @return array <日报列表信息>
     */
    public function getAnnouncementData($where,$page,$limit)
    {
        $count = Db::name('mob_announcement')->where($where)->count();
        // 查询状态为1的用户数据 并且每页显示10条数据
        $announcementList = Db::name('mob_announcement')->where($where)->limit(($page-1)*$limit,$limit)->order('createtime DESC')->select();
        $users = Cache::get('users');
        foreach ($announcementList as &$val) {
            switch ($val['an_enabeld']) {
                case 'Y': $val['an_enabeld'] = '已启用'; break;
                case 'N': $val['an_enabeld'] = '未启用'; break;
            }
            $val['name'] = $users[$val['name']]['cn_name'];
        }
        $rowsList = count($announcementList) ? $announcementList : false;
        $arrData["code"] = 0;
        $arrData["msg"] = '';
        $arrData["count"] = $count;
        $arrData["data"] = $rowsList;
        return $arrData;
    }


    /**
     * 发布公告
     * @param  array $data 新增数据
     * @return json        新增状态
     */
    public function insertAnnouncement ($data)
    {
        $result = Db::name('mob_announcement')->insert($data);
        // print_r($result);exit;
        if ($result){
            echo json_encode(array('success'=>true,'msg'=>'发布成功！'));
        } else {
            echo json_encode(array('msg'=>'发布失败！'));
        }
    }


}